package org.iside.android.showcase;

import java.util.Map;

import org.iside.android.showcase.utils.FileUtils;
import org.springframework.android.showcase.location.BMapApiDemoApp;
import org.springframework.android.showcase.push.MQTTService;
import org.springframework.security.crypto.encrypt.AndroidEncryptors;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.sqlite.SQLiteConnectionRepository;
import org.springframework.social.connect.sqlite.support.SQLiteConnectionRepositoryHelper;
import org.springframework.social.connect.support.ConnectionFactoryRegistry;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.connect.TwitterConnectionFactory;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.widget.Toast;

public class MainApplication extends BMapApiDemoApp {
    private SharedPreferences settings;
    
    private ConnectionFactoryRegistry connectionFactoryRegistry;

    private SQLiteOpenHelper repositoryHelper;

    private ConnectionRepository connectionRepository;
    
    private static String fullAppPath;
    
    // ***************************************
    // Application Methods
    // ***************************************
    @Override
    public void onCreate() {
        super.onCreate();
        
        settings = getSharedPreferences("APP_SETTING", Application.MODE_PRIVATE);
        
        fullAppPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/hi_taxi";
        FileUtils.createSDDir(fullAppPath);
        
        // create a new ConnectionFactoryLocator and populate it with Facebook
        // and Twitter ConnectionFactories
        connectionFactoryRegistry = new ConnectionFactoryRegistry();
        connectionFactoryRegistry.addConnectionFactory(new FacebookConnectionFactory(getFacebookAppId(),
                getFacebookAppSecret()));
        connectionFactoryRegistry.addConnectionFactory(new TwitterConnectionFactory(getTwitterConsumerToken(),
                getTwitterConsumerTokenSecret()));

        // set up the database and encryption
        repositoryHelper = new SQLiteConnectionRepositoryHelper(this);
        connectionRepository = new SQLiteConnectionRepository(repositoryHelper, connectionFactoryRegistry,
                AndroidEncryptors.text("password", "5c0744940b5c369b"));
        
        Intent svc = new Intent(this, MQTTService.class);
        startService(svc);
    }

    // ***************************************
    // Private methods
    // ***************************************
    private String getFacebookAppId() {
        return getString(R.string.facebook_app_id);
    }

    private String getFacebookAppSecret() {
        return getString(R.string.facebook_app_secret);
    }

    private String getTwitterConsumerToken() {
        return getString(R.string.twitter_consumer_key);
    }

    private String getTwitterConsumerTokenSecret() {
        return getString(R.string.twitter_consumer_key_secret);
    }

    // ***************************************
    // Public methods
    // ***************************************
    public ConnectionRepository getConnectionRepository() {
        return connectionRepository;
    }

    public SharedPreferences getSharedPreferences() {
        return settings;
    }
    
    public FacebookConnectionFactory getFacebookConnectionFactory() {
        return (FacebookConnectionFactory) connectionFactoryRegistry.getConnectionFactory(Facebook.class);
    }

    public TwitterConnectionFactory getTwitterConnectionFactory() {
        return (TwitterConnectionFactory) connectionFactoryRegistry.getConnectionFactory(Twitter.class);
    }

    public String getDeviceID() {
        TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = telephonyManager.getDeviceId();
        if (TextUtils.isEmpty(deviceId)) {
            deviceId = getMacAddress();
        }
        return deviceId;
    }

    public String getMacAddress() {
        WifiManager wManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

        WifiInfo wInfo = wManager.getConnectionInfo();
        return "MAC:" + wInfo.getMacAddress();

    }

    public String getSubscriberID() {
        TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        String imsi = telephonyManager.getSubscriberId();
        return imsi;
    }

    public String getVersionName() {
        PackageInfo info = null;
        try {
            info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
            return info.versionName;
        } catch (NameNotFoundException e) {
        }
        return "";
    }
    
    public int getVersionCode() {
        PackageInfo info = null;
        try {
            info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
            return info.versionCode;
        } catch (NameNotFoundException e) {
        }
        return 0;
    }
    
    public String getFullAppPath() {
        return fullAppPath;
    }
    
    public void showToast(CharSequence msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }
}
